import type { TPageResult } from '@/types/global'
import type {
  TOrderPreParams,
  TOrderPre,
  IPostOrderParams,
  IOrderDetailResult,
  ILogisticResult,
  IOrderListItem,
} from '@/types/order'
import { http } from '@/utils/http'

class OrderApi {
  /**
   * @returns 购物车预付款
   */
  public static getOrderPreDetail() {
    return http<TOrderPre>({
      url: '/member/order/pre',
    })
  }
  /**
   * @returns 立即购买预付单
   * @params
   */
  public static getOrderPreDetailNow(data: TOrderPreParams) {
    return http<TOrderPre>({
      url: '/member/order/pre/now',
      method: 'GET',
      data,
    })
  }

  /**
   * @returns 提交订单
   * @params IPostOrderParams
   */
  public static postOrder(data: IPostOrderParams) {
    return http<{ id: string }>({
      url: '/member/order',
      method: 'POST',
      data,
    })
  }

  /**
   * @returns 根据订单id获取订单详情
   * @params id 订单id
   */
  public static getOrderDetailById(id: string) {
    return http<IOrderDetailResult>({
      url: `/member/order/${id}`,
      method: 'GET',
    })
  }
  /**
   * @returns 获取再次购买订单详情
   * @params id 订单id
   */
  public static getOrderRepurchaseById(id: string) {
    return http<TOrderPre>({
      url: `/member/order/repurchase/${id}`,
      method: 'GET',
    })
  }

  /**
   * @returns 获取微信支付参数
   * @params data {orderId} 订单ID
   */
  public static getPayWxPayMiniPayAPI = (data: { orderId: string }) => {
    return http<WechatMiniprogram.RequestPaymentOption>({
      url: '/pay/wxPay/miniPay',
      method: 'GET',
      data,
    })
  }

  /**
   * @returns 模拟支付-内测版
   * @params data {orderId} 订单ID
   */
  public static getPayMockAPI = (data: { orderId: string }) => {
    return http({
      url: '/pay/mock',
      method: 'GET',
      data,
    })
  }

  /**
   * @returns 模拟发货-内测版
   * @params data {id}订单ID
   */
  public static getConsignmentByID = (id: string) => {
    return http({
      url: `/member/order/consignment/${id}`,
      method: 'GET',
    })
  }

  /**
   * @description 确认收货
   * @params id 订单id
   */
  public static putOrderConfirm(id: string) {
    return http<IOrderDetailResult>({
      url: `/member/order/${id}/receipt`,
      method: 'PUT',
    })
  }

  /**
   * @description 获取订单物流
   * @params id 订单id
   */
  public static getOrderLogistic(id: string) {
    return http<ILogisticResult>({
      url: `/member/order/${id}/logistics`,
      method: 'GET',
    })
  }

  /**
   * @description 删除订单
   * @params id 订单id
   */
  public static deleteOrderById(data: { ids: string[] }) {
    return http({ url: '/member/order', method: 'DELETE', data })
  }

  /**
   * @description 取消订单
   * @params id 订单id
   */
  public static cancelOrderById(id: string, data: { cancelReason: string }) {
    return http<IOrderDetailResult>({
      url: `/member/order/${id}/cancel`,
      method: 'PUT',
      data,
    })
  }

  /**
   * @description 获取订单列表
   * @params {page,pageSize,orderState} 页码 页容量 订单状态 enum
   */
  public static getOrderList(data?: { page?: number; pageSize?: number; orderState?: number }) {
    return http<TPageResult<IOrderListItem>>({
      url: '/member/order',
      method: 'GET',
      data,
    })
  }
}

export default OrderApi
